package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.family.Family;

/**
 * 家庭档案管理控制层
 * @ClassName: FamilyServlet
 * @author SunYi
 * @Date 2019年4月28日下午10:14:39
 */
public class FamilyServlet extends BaseServlet {

	private static final long serialVersionUID = 2156046758236335189L;

	// 覆写doGet方法
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 把请求转给doPost方法处理
		this.doPost(request, response);
	}

	// 覆写doPost方法
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 获取页面表单提交的方法名称
		String m = req.getParameter("m");
		// 根据页面表单提交的方法名称请求转发到相应的页面
		// 根据方法名进行请求转发
		if("list".equals(m)){
			process(req, resp, "/page/family/family_list.jsp");
		}else if("input".equals(m)){
			process(req, resp, "/page/family/family_add.jsp");
		}else if("get".equals(m)){
			process(req, resp, "/page/family/family_edit.jsp");
		}else if("add".equals(m)){
			add(req, resp);
			process(req, resp, "/page/family/family_list.jsp");
		}else if("del".equals(m)){
			del(req, resp);
		} else if("edit".equals(m)){
			edit(req, resp);
		} else {
			error(req, resp);
		}
	}
	
	/**
	 * 添加家庭档案
	 * @author SunYi
	 * @Date 2019年4月28日下午11:24:18
	 * @return void
	 */
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		// 接收表单传过来的参数
		String areacode = req.getParameter("areacode");
		// 将行政区域树的节点进行分割
		String[] familyAreacode = areacode.split(",");
		String countyCode = null;
		String townCode = null;
		String villageCode = null;
		String groupCode = null;
		// 遍历分割好的行政区域树数组，得到每个行政级别的编码
		for (int i = 0; i < familyAreacode.length; i++) {
			countyCode = familyAreacode[0];
			townCode = familyAreacode[1];
			villageCode = familyAreacode[2];
			groupCode = familyAreacode[3];
		}
		String familyCode = req.getParameter("familyCode");
		String houseHolder = req.getParameter("houseHolder");
		int familySize = Integer.parseInt(req.getParameter("familySize"));
		int agriculturalNum = Integer.parseInt(req.getParameter("agriculturalNum"));
		String address = req.getParameter("address");
		String registrar = req.getParameter("registrar");
		String householdType = req.getParameter("householdType");
		try{
			// 获取当前系统时间作为家庭档案创建时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date createTime = sdf.parse(sdf.format(new Date()));
			Family model = new Family(countyCode, townCode, villageCode, groupCode, familyCode, houseHolder, familySize, agriculturalNum, address, createTime, registrar, householdType);
			// 调用预添加方法
			model.toAddFamily();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新家庭档案信息
	 * @author SunYi
	 * @Date 2019年4月29日上午10:40:53
	 * @return void
	 */
	private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		// 接收表单传过来的参数
		String familyCode = req.getParameter("familyCode");
		String houseHolder = req.getParameter("houseHolder");
		int familySize = Integer.parseInt(req.getParameter("familySize"));
		int agriculturalNum = Integer.parseInt(req.getParameter("agriculturalNum"));
		String address = req.getParameter("address");
		String registrar = req.getParameter("registrar");
		String householdType = req.getParameter("householdType");
		try {
			Family model = new Family(familyCode, houseHolder, familySize, agriculturalNum, address, registrar, householdType);
			boolean flag = model.toUpdateFamily();
			if(flag) {
				resp.getWriter().write("true");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除家庭档案信息
	 * @author SunYi
	 * @Date 2019年4月29日下午1:39:58
	 * @return void
	 */
	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		// 接收表单传过来的数据
		String familyCode = req.getParameter("familyCode");
		try {
			Family family = new Family();
			boolean flag = family.toDeleteFamily(familyCode);
			if(flag) {
				resp.getWriter().write("true");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
